<script>
import outMain from "@/components/outMain.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { AccreditationSign, checkStatus, signExcel } from "@/http/api";
import * as XLSX from "xlsx";
import { saveAs } from "file-saver";
export default {
  components: { outMain },
  name: "",
  data() {
    return {
      tableData: [],
      selectArr: [],
      statusArr: [
        { label: "全部", value: "" },
        { label: "待付款", value: "1" },
        { label: "待发货", value: "2" },
        { label: "已完成", value: "3" },
        { label: "已取消", value: "4" },
      ],
      queryData: {
        pay_status: "",
        real_name: "",
        accreditation_name: "",
        page: 1,
        per_page: 10,
      },
      total: 0,
      headArr: [
        { name: "订单号", key: "sign_number" },
        { name: "办证名称", key: "accreditation_name" },
        { name: "办证费用", key: "pay_money" },
        { name: "姓名", key: "real_name" },
        { name: "手机号", key: "mobile" },
        { name: "身份证号", key: "id_card" },
        { name: "收件人", key: "zs_shr" },
        { name: "收件人电话", key: "zs_mobile" },
        { name: "收件城市", key: "zs_city" },
        { name: "收件详细地址", key: "zs_address" },
        { name: "海事局密码", key: "hsj_password" },
        { name: "创建时间", key: "create_time" },
      ],
    };
  },
  props: {},
  setup() {},
  methods: {
    exportFn() {
      if (this.selectArr.length == 0) {
        ElMessage.error("请选择需要导出的订单");
        return false;
      }
      let ids = this.selectArr.map((item) => {
        return item.user_accreditation_sign_id;
      });
      let data = {
        user_accreditation_sign_id: ids.join(","),
      };
      signExcel(data).then((res) => {
        if (res.data.code == 1) {
          let arr = res.data.data;
          let headArr = this.headArr.map((item) => {
            return item.name;
          });
          let exportArr = [headArr];
          for (let item of arr) {
            let itemArr = [];
            for (let a of this.headArr) {
              itemArr.push(item[a.key]);
            }
            exportArr.push(itemArr);
          }
          this.exportToExcel(exportArr);
        }
      });
    },
    exportToExcel(data) {
      // 将数据转换为工作表
      const worksheet = XLSX.utils.aoa_to_sheet(data);

      // 创建工作簿并添加工作表
      const workbook = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
      // 生成Excel文件
      const excelBuffer = XLSX.write(workbook, {
        bookType: "xlsx",
        type: "array",
      });

      // 使用blob和saveAs下载文件
      const dataBlob = new Blob([excelBuffer], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8",
      });
      saveAs(dataBlob, "export.xlsx");
    },
    detailFn(id) {
      this.$router.push({ path: "/workSignDetail", query: { id: id } });
    },
    checkFn(row, type) {
      let str = type == 3 ? "接单" : "完结";
      let strStatuts = type == 3 ? "办理中" : "已完成";
      ElMessageBox.confirm(
        `确定${str}选择的订单吗?点击确定订单状态将转化为${strStatuts}`,
        "操作提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        checkStatus({
          user_accreditation_sign_id: row.user_accreditation_sign_id,
          status: type,
        }).then((res) => {
          if (res.data.code == 1) {
            ElMessage({
              type: "success",
              message: "操作成功",
            });
            this.getData();
          }
        });
      });
    },
    getData() {
      AccreditationSign(this.queryData).then((res) => {
        if (res.data.code == 1) {
          this.tableData = res.data.data.data;
          this.total = res.data.data.total;
        }
      });
    },
    handleSelectionChange(value) {
      this.selectArr = value;
    },
  },
  mounted() {},
  created() {
    this.getData();
  },
};
</script>
<template>
  <outMain>
    <div class="tableCont">
      <div class="tableName flexCont">
        <div>办证订单({{ total }})</div>
      </div>
      <div class="tableSerch">
        <div class="serchLeft"></div>
        <div class="serchRight">
          <el-form class="flexCenter" :inline="true">
            <el-form-item>
              <el-input
                clearable
                @clear="getData"
                @keyup.enter="getData"
                v-model="queryData.accreditation_name"
                placeholder="请输入证件名称"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                clearable
                @clear="getData"
                @keyup.enter="getData"
                v-model="queryData.real_name"
                placeholder="请输入姓名"
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryData.pay_status"
                clearable
                filterable
                allow-create
                @change="getData"
                placeholder="请选择缴费情况"
              >
                <el-option
                  v-for="item in statusArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <btn value="搜索" @click="getData"></btn>
            <btn value="导出" class="hasLeft" @ok="exportFn"></btn>
          </el-form>
        </div>
      </div>
      <el-table
        class="tableMain"
        @selection-change="handleSelectionChange"
        :data="tableData"
        stripe
        style="width: 100%"
      >
        <el-table-column type="selection" width="30" />
        <el-table-column
          prop="sign_number"
          width="160"
          show-overflow-tooltip
          label="订单编号"
        />
        <el-table-column
          width="80"
          prop="real_name"
          show-overflow-tooltip
          label="姓名"
        />
        <el-table-column
          width="130"
          prop="mobile"
          show-overflow-tooltip
          label="手机号"
        />
        <el-table-column
          prop="id_card"
          width="180"
          show-overflow-tooltip
          label="身份证号"
        />
        <el-table-column
          prop="accreditation_name"
          show-overflow-tooltip
          width="150"
          label="办证名称"
        />
        <el-table-column show-overflow-tooltip label="订单状态">
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tag type="danger" v-if="row.pay_status == 1">待支付</el-tag>
              <el-tag type="warning" v-if="row.pay_status == 2">待接单</el-tag>
              <el-tag v-if="row.pay_status == 3">办理中</el-tag>
              <el-tag type="success" v-if="row.pay_status == 4">已完成</el-tag>
              <el-tag type="info" v-if="row.pay_status == 5">已取消</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="accreditation_money"
          show-overflow-tooltip
          label="订单金额"
        >
          <template #default="{ row }"
            >{{ row.accreditation_money }}元</template
          >
        </el-table-column>
        <el-table-column
          prop="create_time"
          width="170"
          show-overflow-tooltip
          label="创建时间"
        />
        <el-table-column prop="Name" width="160" label="操作">
          <template #default="{ row }">
            <div class="tableCtrl">
              <div
                class="greenText ctrlText"
                @click="detailFn(row.user_accreditation_sign_id)"
              >
                详情
              </div>
              <div
                class="blueText ctrlText"
                v-if="row.pay_status == 2"
                @click="checkFn(row, 3)"
              >
                接单
              </div>
              <div
                class="blueText ctrlText"
                v-if="row.pay_status == 3"
                @click="checkFn(row, 4)"
              >
                已处理
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageCont">
        <el-pagination
          v-model:currentPage="queryData.page"
          v-model:page-size="queryData.per_page"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          @size-change="getData"
          @current-change="getData"
        />
      </div>
    </div>
  </outMain>
</template>
<style scoped lang="less"></style>
